<template>
  <div class='box'>
    <a-card>
      <h3>添加轮播图信息</h3>
      <a-form-model >
        <a-form-model-item label='轮播图名称' prop='name'>
          <a-input style='width: 300px' v-model='bannerInfo.name'></a-input>
        </a-form-model-item>

        <a-form-model-item label='文章缩略图' prop='pic'>
          <a-upload listType='picture' name='file' :action='upUrl' :headers='headers'
                    @change='upChange'>
            <a-button>
              <a-icon type='upload' />
              点击上传
            </a-button>
            <template v-if='bannerInfo.pic !== ""'>
              <img :src='bannerInfo.pic' style='width: 120px; height: 100px; margin-left: 15px'  alt=''/>
            </template>
          </a-upload>
          <a-form-model-item>
            <a-button type='danger' style='margin-right: 15px' @click='artOk'>
              提交
            </a-button>
          </a-form-model-item>
        </a-form-model-item>
      </a-form-model>
    </a-card>

    <a-card>
      <h3>轮播图列表</h3>
      <a-button @click="getBannerList" >请求列表</a-button>
      <a-table :dataSource="dataSource" :columns="columns" rowKey="ID">


      </a-table>
    </a-card>
  </div>
</template>

<script>
import { Url } from '@/plugin/http'

export default {
  data() {
    return {
      bannerInfo: {
        pic: '',
        name: '',
      },
      upUrl: Url + 'upload',
      headers: {},
      dataSource: [],
      columns: [
        {
          title: '图片',
          dataIndex: 'pic',
          key: 'pic',
          align: 'center',
          scopedSlots: { customRender: 'pic' },
        },
        {
          title: '名字',
          dataIndex: 'name',
          key: 'name',
        },
      ]
    }
  },
  methods: {
    // 上传图片
    upChange(info) {
      if (info.file.status !== 'uploading') {
      }
      if (info.file.status === 'done') {
        this.$message.success(`图片上传成功`)
        this.bannerInfo.pic = info.file.response.url
      } else if (info.file.status === 'error') {
        this.$message.error(`图片上传失败`)
      }
    },
    artOk() {
      const result = this.$http.post('/add_banner', this.bannerInfo)
      result.then(res => {
        if (res.status !== 200) return this.$message.error(res.message)
        this.$message.success('添加轮播图成功')
      })
    },
    // 请求列表信息
    async getBannerList() {
      const {data:res} = await this.$http.get('/banner_list')
      if (res.status !== 200) {
        this.$message.error(res.message)
      }
      this.dataSource = res.data
    }
  },
  //加载视图的时候请求列表数据
  created() {
    this.getBannerList()
  }
}
</script>

